// load nodes from the database on xml format
function loadnodes() {
	var ratings = document.formnode.ratings.value;
	var nodes = document.formnode.nodes.value;
	var distnc= document.formnode.distance.value;
	var dist=0;
	
	// linkbox
	if (nodes=="allnodes") {
		document.getElementById("checkbox").innerHTML= "Links : <input type='checkbox' id='box' onclick='boxlink(this)'></input><br>";	
	}
	else {
		document.getElementById("checkbox").innerHTML = "";
	}
	
	
	if (distnc=="select") {
		;
	}
	else if (distnc=="a1") {
			dist=1;
		}
	else if (distnc=="a2") {
			dist=2;
		}
	else if (distnc=="a3") {
			dist=3;
		}
	else if (distnc=="a4") {
			dist=4;
		}
	else if (distnc=="a5") {
			dist=5;
		}
	else if (distnc=="a6") {
			dist=6;
		}
	else if (distnc=="a8") {
			dist=8;
		}
	else if (distnc=="a10") {
			dist=10;
		}
		
	var loadxml="";

	if (ratings=="select") {
		if (nodes=="freenodes") {
			loadxml="mapxml.jsp?choice=freenorate&nodename="+init_nodename+"&rate=";
		}
		else if (nodes=="allnodes") {
			loadxml="mapxml.jsp?choice=allnorate&nodename="+init_nodename+"&rate=";
		}
	} // end if
	else if (ratings!="select") {
		if (nodes=="freenodes") {
			loadxml="mapxml.jsp?choice=freerate&nodename="+init_nodename+"&rate="+ratings;
		}
		else if (nodes=="allnodes") {
			loadxml="mapxml.jsp?choice=allrate&nodename="+init_nodename+"&rate="+ratings;
		}
	} // end else
	
	//clear links
	hidelink();
	//clear nodes
	hide("nodes");
	clearnodes();
	
	document.getElementById("side_bar1").innerHTML = "loading...";
	
	//Start with an empty GLatLngBounds object  
	   
  	var bounds = new GLatLngBounds();
	var xmlhttp;
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
	var xmlDoc = xmlhttp.responseXML;
	for (var i=0;i < xmlDoc.getElementsByTagName("lat").length;i++) {
		a = xmlDoc.getElementsByTagName("lat")[i].childNodes[0].nodeValue;
		b = xmlDoc.getElementsByTagName("lng")[i].childNodes[0].nodeValue;
		c = xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue;	
		d = xmlDoc.getElementsByTagName("owner")[i].childNodes[0].nodeValue;
		
	bounds.extend(init_p);
	point = new GLatLng(a,b);
	var dstnc = distance(init_p,point);
			var html = "<b>Name :</b> " + c + "<br>" + "<b>Owner :</b> " + d + "<br>" + "<b>Distance :</b>" + dstnc.toPrecision(3) + "Km";
	if (dist>0) {
		if (dstnc<=dist) {
			var marker = createMarker(point,c,"nodes",html);
			map.addOverlay(marker);
		 	bounds.extend(point);
		}
	}
	else if (dist==0){	
			var marker = createMarker(point,c,"nodes",html);
			map.addOverlay(marker);
		    bounds.extend(point);
	}
	}//for
	map.setZoom(map.getBoundsZoomLevel(bounds));
	map.setCenter(bounds.getCenter());
	document.getElementById("side_bar1").innerHTML = "";
	makeSidebar1();
	}
	}
	xmlhttp.open("GET",loadxml,true);
	xmlhttp.send();	
}

function loadtabinfo(nodename,html1) {
	var xmlreq;
	if (window.XMLHttpRequest)
	  {
	  xmlreq=new XMLHttpRequest();
	  }
	else // IE 5/6
	  {
	  xmlreq=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlreq.onreadystatechange=function() {
	if (xmlreq.readyState==4 && xmlreq.status==200) {
	var xmlDoc = xmlreq.responseText;
		document.getElementById("info").innerHTML = xmlDoc;
		document.getElementById("info2").innerHTML = document.getElementById("tab2").innerHTML;
		document.getElementById("info3").innerHTML = document.getElementById("tab3").innerHTML;
		document.getElementById("info1").innerHTML = '<div style="width:'+3*98+'px">' + html1 + '</div>' + document.getElementById("tab1").innerHTML;
	}
	}
	xmlreq.open("GET","infonode.jsp?keynode="+init_key+"&nodename1="+nodename,true);
	xmlreq.send();
} // xml


//xml
function loadXMLDoc(dname) {
	if (window.XMLHttpRequest)
	  {
	  xhttp=new XMLHttpRequest();
	  }
	else // IE 5/6
	  {
	  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xhttp.open("GET",dname,false);
	xhttp.send();
	return xhttp.responseXML;
} // xml


function distance(p1,p2) {
	var d = p1.distanceFrom(p2);
	var kil = d/1000;
	return kil;
}


function loadlink(url) {
		
	hidelink();
	var j = -1;
	//scan links


	document.getElementById("showlinks").innerHTML = "loading...";
	var xmlhttp;
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
	var xmlDoc = xmlhttp.responseXML;
	
	for (var i=0;i < xmlDoc.getElementsByTagName("point1lat").length;i++) {
		g = xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue;
		c = xmlDoc.getElementsByTagName("point1lat")[i].childNodes[0].nodeValue;
		d = xmlDoc.getElementsByTagName("point1lng")[i].childNodes[0].nodeValue;
		e = xmlDoc.getElementsByTagName("point2lat")[i].childNodes[0].nodeValue;
		f = xmlDoc.getElementsByTagName("point2lng")[i].childNodes[0].nodeValue;
		l = xmlDoc.getElementsByTagName("freq")[i].childNodes[0].nodeValue;
		p = xmlDoc.getElementsByTagName("power")[i].childNodes[0].nodeValue;	
		
		var point3 = new GLatLng(c,d);
		var point4 = new GLatLng(e,f);
		var line = new GPolyline([point3,point4],"",3);
		var distnc = distance(point3,point4);				
		line.html = "<b>link :</b> " + g + "<br>" + "<b>frequency :</b> " + l +"GHz"+ "<br>" + "<b>distance :</b> " + distnc.toPrecision(3) + "Km";;
		linkinfo.push(line);
		line.show();
		j++;
		linewindow(linkinfo[j],j);
		map.addOverlay(line);
		document.getElementById("showlinks").innerHTML = "";
	}
	}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}

function linewindow(line,j) {
	
	GEvent.addListener(line,"click",function(point){
		var mp = new GLatLng(point.lat(),point.lng()); 
		var marker = new GMarker(mp);
		html = line.html;
		map.openInfoWindowHtml(mp,html);
	});
}


function hide(category) {
        for (var i=0; i<gmarkers.length; i++) {
          if (gmarkers[i].mycategory == category) {
            gmarkers[i].hide();
          }
        }      
	   map.closeInfoWindow();
}

function hidelink() {
	for (var i=0;i<linkinfo.length;i++) {
		linkinfo[i].hide();
	}
	if (linkinfo.length!=0) { 
		linkinfo.splice(0,linkinfo.length);
	}
}

function mylink(i) {
	GEvent.trigger(gmarkers[i], "click");
}

function myclick(i) {
	GEvent.trigger(gmarkers[i], "click");
	
}

function myclicknode(node){
	GEvent.trigger(node, "click");
}

function makeSidebar0() {
    var html = "";
	var side_html=["",""];
    for (var i=0; i<gmarkers.length; i++) {
			if (gmarkers[i].mycategory=="initp") {
				side_html[0] += '<br>' + '<a href="javascript:myclick(' + i + ')" onmouseover="mymouseover('+i+')" onmouseout="mymouseout()">' + gmarkers[i].myname + '<\/a>';	
			}
			else if (gmarkers[i].mycategory=="mylinks") {
				side_html[1] += '<br>' + '<a href="javascript:myclick(' + i + ')" onmouseover="mymouseover('+i+')" onmouseout="mymouseout()">' + gmarkers[i].myname + '<\/a>';	
			}		
	}
			document.getElementById("side_bar0").innerHTML = "My Node" + side_html[0] + "<p>" + "My Links" + side_html[1];
}
	
	
function makeSidebar1() {
	var html = "";
	var side_html=["",""];
    for (var i=0; i<gmarkers.length; i++) {
	if (!gmarkers[i].isHidden()) {
			if (gmarkers[i].mycategory=="nodes") {
				side_html[0] += '<br>' + '<a href="javascript:myclick(' + i + ')" onmouseover="mymouseover('+i+')" onmouseout="mymouseout()">' + gmarkers[i].myname + '<\/a>';	
			}		
	}
    }		
			document.getElementById("side_bar1").innerHTML = side_html[0] + side_html[1];
}

function mymouseover(i) {
        showTooltip(gmarkers[i])
}

function mymouseout() {
	tooltip.style.visibility="hidden";
}

 function showTooltip(marker) {
   	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
}

function addlink() 
{ 
    mywindow = window.open("","myNewWin","width=500,height=350,toolbar=0");
 	mywindow.moveTo(100,100);
    var a = window.setTimeout("document.form3.submit();",500);
}

function sendme() 
{ 
    mywindow = window.open("","myNewWin","width=500,height=350");
 	mywindow.moveTo(100,100);
    var a = window.setTimeout("document.form1.submit();",500); 
}

function sendrating() 
{ 
    mywindow = window.open("","myNewWin","width=500,height=300");
 	mywindow.moveTo(100,100);
    var a = window.setTimeout("document.form2.submit();",500); 
}

function openwindow(url) {
	mywindow = window.open(url,"myNewWin","width=500,height=350");
 	mywindow.moveTo(200,200);
    var a = window.setTimeout("document.form2.submit();",500);
}

function clearnodes() {
	for (var i=0; i<gmarkers.length; i++) {
		if (gmarkers[i].mycategory!="nodes") {
			//gmarkers.splice(i,1);
			var j=i;
		}
	}
	j=j+1;
	gmarkers.splice(j,gmarkers.length-j);
}


function createtest() {	
	var point1 = new GLatLng(38, 23);
	var x = map.getCurrentMapType().getProjection().fromLatLngToPixel(point1,8).x;
	var y = map.getCurrentMapType().getProjection().fromLatLngToPixel(point1,8).y;
	
	var t = new Array();
	t[0] = "ok";
	t[1] = "test";
	document.getElementById("dist").innerHTML = test(t)[1];
	
}

function boxlink(box){
	if (box.checked){
		loadlink("linkxml.jsp");
	}
	else {
		hidelink();
	}
}

function showlinks(name1,name2) {
	loadlink("linkneighbor.jsp?node1="+name1+"&node2="+name2);
}

function sendmessage(name1, name2) {
	mywindow = window.open("msgform.jsp?fromnode="+name1+"&tonode="+name2+"&typeofmsg=newmsg","myNewWin","width=500,height=500");
 	mywindow.moveTo(100,100);
    var a = window.setTimeout("document.form1.submit();",500);
}

function addinterface(name) {
	mywindow = window.open("addif?nodename="+name+"&action=new","myNewWin","width=500","height=500");
 	mywindow.moveTo(100,100);
    var a = window.setTimeout("document.form1.submit();",500);
}

function test(p){
	var s = new Array();
	s[0] = p[1];
	s[1] = p[0];
	return s;
}

function dist(p1,p2) {

	var distance = Math.sqrt((p1[0]-p2[0])*(p1[0]-p2[0])+(p1[1]-p2[1])*(p1[1]-p2[1]));
	return distance;
}	

	var polyline = new GPolyline(
		[new GLatLng(37.4419, -122.1419),new GLatLng(37.4519, -122.1519)], 
		"#ff0000", 10);
	map.addOverlay(polyline);

/////////////////////
function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-150;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-150;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}

function changestar() {
	document.getElementById("com").style.width=50+'%';
}
	